home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 17707 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.5 KB  |  185 lines

  1. Path: ix.netcom.com!news
  2. From: lewkbj@ix.netcom.com (leonel wizel )
  3. Newsgroups: comp.lang.c++
  4. Subject: help with homework
  5. Date: 17 Apr 1996 02:13:06 GMT
  6. Organization: Netcom
  7. Message-ID: <4l1k3i$kle@dfw-ixnews2.ix.netcom.com>
  8. NNTP-Posting-Host: ix-nyc23-13.ix.netcom.com
  9. X-NETCOM-Date: Tue Apr 16  9:13:06 PM CDT 1996
  10.  
  11.  
  12.     Please I need help to complete this file:   this file suppose to
  13. increment the object time by one second to the next hour, to the
  14. next minute to the next day:
  15.  
  16. I need some help please:
  17.  
  18.  
  19. the program suppose to modify to include a tick member function
  20. that increments the time stored in the Time object by one second.  The
  21. time object should always remain in a consistent state.  The driver
  22. program that tests the tick member function in a loop that prints the
  23. time in standard format during each iteration of the loop to illustrate
  24. that the tick member functions workds correctly.  
  25.  
  26.     Incrementing into the next minute.
  27.     Incrementing into the next hour.
  28.     incrementing into the next day(i.e., 11:59:59 PM to 12:00:00 AM).
  29.  
  30.  
  31. //TIME3.H
  32.  
  33. #ifndef TIME3_H
  34. #define TIME3_H
  35.  
  36.  
  37. class Time {
  38. public:
  39.     Time(int = 0, int = 0, int = 0);  // constructor
  40.  
  41.     //set functions
  42.  
  43.     void setTime(int, int, int); //set hour, minute, second
  44.     void setHour(int);  //set hour
  45.     void setMinute(int); //set minute
  46.     void setSecond(int); //set second
  47.  
  48.     //get functions
  49.  
  50.     int getHour();   //return hour
  51.     int getMinute(); //return minute
  52.     int getSecond(); //return second
  53.  
  54.     void tick_increment();
  55.     void printMilitary();   //output military time
  56.     void printStandard();   //output standard time
  57.  
  58. private:
  59.     int hour;          //0 - 23
  60.     int minute;        //0 - 59
  61.     int second;        //0 - 59
  62. };
  63.  
  64.  
  65. #endif
  66.  
  67.  
  68. #include "time3.h"
  69. #include <iostream.h>
  70.  
  71.  
  72. //constructor function
  73.  
  74. Time::Time(int hr, int min, int sec) {setTime (hr, min, sec); }
  75.  
  76. void Time::setTime( int h, int m, int s)
  77. {
  78.     hour = (h >= 0 && h < 24) ? h : 0;
  79.     minute = (m >= 0 && m < 60) ? m : 0;
  80.     second = (s >= 0 && s < 60) ? s : 0;
  81. }
  82.  
  83. void Time::setHour(int h)  { hour = (h >= 0 && h < 24) ? h : 0;}
  84.  
  85. void Time::setMinute(int m)
  86.     { minute = (m >= 60) ? m : 0; }
  87.  
  88. void Time::setSecond(int s)
  89.     { second = (s >= 60) ? s : 0; }
  90.  
  91.  
  92. int Time::getHour() {return hour; }
  93.  
  94. int Time::getMinute() {return minute; }
  95.  
  96. int Time::getSecond() {return second; }
  97.  
  98.  
  99. void Time::tick_increment()
  100. {
  101.     if(second == 60)
  102.     {
  103.         second = 0;
  104.         minute++;
  105.     }
  106.         second++;
  107.         if (second == 60)
  108.     {
  109.         second = 0;
  110.         minute++;
  111.     }
  112.         if(minute == 60)
  113.     {
  114.             minute = 0;
  115.             hour++;
  116.     }
  117.         if (hour == 24)
  118.     {
  119.             hour %= 24;
  120.     }
  121. }
  122. void Time::printMilitary()
  123. {
  124.     cout <<  (hour < 10 ? "0" : "") << hour << ":"
  125.           << (minute < 10 ? "0" : "") << minute << ":"
  126.           << (second < 10 ? "0" : "") << second;
  127. }
  128.  
  129. void Time::printStandard()
  130. {
  131.     cout << ((hour == 0 || hour == 12) ? 12 : hour %12) << ":"
  132.           << (minute < 10 ? "0" : "") << minute << ":"
  133.           << (second < 10 ? "0" : "") << second
  134.           << (hour < 12 ? " AM" : " PM");
  135. }
  136.  
  137. #include <iostream.h>
  138. #include "time3.h"
  139. #include "time3.cpp"
  140.  
  141.  
  142. main()
  143. {
  144.   Time t;
  145.  
  146.   t.setTime(23, 59, 59);
  147.  
  148.   for(long int i = 1; i <= 2; i++)
  149.     {
  150.         cout << endl << endl;
  151.         cout << " Hour " << t.getHour()
  152.               << " Minute " << t.getMinute()
  153.               << " Second " << t.getSecond();
  154.  
  155.         cout <<"the initial time before incrementing is: " << endl
  156. << endl;
  157.  
  158.         t.printStandard();
  159.         cout << endl;
  160.         t.printMilitary();
  161.  
  162.         t.tick_increment();
  163.         cout << " Hour " << t.getHour()
  164.               << " Minute " << t.getMinute()
  165.               << " Second " << t.getSecond()
  166.               <<" the time after incrementing is: " << endl;
  167.  
  168.         t.printStandard();
  169.         cout << endl;
  170.         t.printMilitary();
  171.  
  172.     }
  173.  
  174.  
  175.  
  176.     return 0;
  177. }
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.